
cat('Creating Table A23 and Figures A2, A3, and 5.6 \n\n')

dat = read.csv('TableA23FigureA2A3_5_6_data.csv')

non.south.dat = dat[dat$south == 0,]


reg1 = lm(trump.shift~hisp.pct.change.2000.2014,
          data = dat,
          weight = dat$ABBAE001)

reg2 = lm(trump.shift~hisp.pct.change.2000.2014+state_abbr,
          data = dat,
          weight = dat$ABBAE001)

reg3 = lm(trump.shift~hisp.pct.change.2000.2014,
          data = non.south.dat,
          weight = non.south.dat$ABBAE001)

reg4 = lm(trump.shift~hisp.pct.change.2000.2014+state_abbr,
          data = non.south.dat,
          weight = non.south.dat$ABBAE001)


outtable = apsrtable(reg1,
                     reg2,
                     reg3,
                     reg4,
                     Sweave = T,
                     notes = '',
                     stars = 'default'
      )
writeLines(
  outtable, 'TableA23.tex')



##scatterplots
trump.mean = mean(dat$trump.shift)
hisp.med = median(dat$hisp.pct.change.2000.2014, na.rm = T)
out.plot = ggplot(dat, aes(hisp.pct.change.2000.2014,trump.shift)) +
  geom_hline(yintercept = 0, color = 'darkgrey', size = 1) +
  geom_vline(xintercept = 0, color = 'darkgrey', size = 1) +
  geom_smooth(method='lm', color = 'black') +
  geom_point(aes(size = ABBAE001), alpha = .5) +
  scale_size_continuous(range = c(.5, 20), breaks=pretty_breaks(20), guide = FALSE) + 
  labs(x = 'Percent change Hispanic', y = 'Trump shift (from Romney 2012)') + 
  scale_x_continuous(labels = percent) +
  coord_cartesian(xlim= c(-1,6),ylim = c(-.15,.225)) +
  theme(
    axis.text=element_text(size=7.5),
    axis.title=element_text(size=10)
  ) 

ggsave('Figure5_6.jpeg',plot = out.plot,
dpi = 1200,
 width = 4.66,
 height = 4.66,
 unit = 'in') 



mccain.mean = mean(dat$mccain.shift)
hisp.med = median(dat$hisp.pct.change.2000.2006, na.rm = T)
out.plot = ggplot(dat, aes(hisp.pct.change.2000.2006,mccain.shift)) +
  geom_smooth(method='lm', color = 'black') +
  geom_point(aes(size = ABBAE001), alpha = .5) +
  scale_size_continuous(range = c(.5, 20), breaks=pretty_breaks(20), guide = FALSE) + 
  labs(x = 'Percent Change Hispanic', y = 'McCain Shift (from Bush 2004)') + 
  scale_x_continuous(labels = percent) +
  coord_cartesian(xlim= c(-1,6),ylim = c(-.15,.225)) +
  theme(
    axis.text=element_text(size=7.5),
    axis.title=element_text(size=10)
  ) 

ggsave('FigureA2.jpeg',plot = out.plot,
       dpi = 600) 


romney.mean = mean(dat$romney.shift)
hisp.med = median(dat$hisp.pct.change.2000.2014, na.rm = T)
out.plot = ggplot(dat, aes(hisp.pct.change.2000.2014,romney.shift)) +
  geom_smooth(method='lm', color = 'black') +
  geom_point(aes(size = ABBAE001), alpha = .5) +
  scale_size_continuous(range = c(.5, 20), breaks=pretty_breaks(20), guide = FALSE) + 
  labs(x = 'Percent Change Hispanic', y = 'Romney Shift (from McCain 2008)') + 
  scale_x_continuous(labels = percent) +
  coord_cartesian(xlim= c(-1,6),ylim = c(-.15,.225)) +
  theme(
    axis.text=element_text(size=7.5),
    axis.title=element_text(size=10)
  ) 

ggsave('FigureA3.jpeg',plot = out.plot,
  dpi = 600) 



